//InnerAudioContext实例
const app = getApp()
//InnerAudioContext实例
var audioCxt;
//动画
var audioAnimation;
audioCxt = wx.createInnerAudioContext();
audioCxt.src = 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/kata.mp3';
var startPoint;
Page({
  data: {
    windowHeight: '',
    windowWidth: '',
    top1: 65,
    left1: 50,
    top2: 60,
    left2: 240,
    top3: 240,
    left3: 50,
    top4: 240,
    left4: 240,
    top4ok: 65,
    left4ok: 50,
    top3ok: 60,
    left3ok: 240,
    top2ok: 240,
    left2ok: 50,
    top1ok: 240,
    left1ok: 240,
    count:0,
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  onLoad:function(){
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          //buttonTop1:buttonTop1,//这里定义按钮的初始位置
          //buttonLeft:res.windowWidth*0.70,//这里定义按钮的初始位置
        })
      }
    })
  },
  //以下是按钮拖动事件
  buttonStart: function (e) {
    startPoint = e.touches[0]//获取拖动开始点
    console.log('buttonStart======startPoint'+JSON.stringify(startPoint))
  },
  buttonMove: function (e) {
    let pen = e.currentTarget.dataset.pen;
    //console.log('move '+pen)
    var endPoint = e.touches[e.touches.length - 1]//获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint//重置开始位置
    var buttonTop = this.data.top1 + translateY
    var buttonLeft = this.data.left1 + translateX
    if(pen==2){
      buttonTop = this.data.top2 + translateY
      buttonLeft = this.data.left2 + translateX
    }
    else if(pen==3){
      buttonTop = this.data.top3 + translateY
      buttonLeft = this.data.left3 + translateX
    }
    else if(pen==4){
      buttonTop = this.data.top4 + translateY
      buttonLeft = this.data.left4 + translateX
    }
    //判断是移动否超出屏幕
    if (buttonLeft+50 >= this.data.windowWidth){
      buttonLeft = this.data.windowWidth-50;
    }
    if (buttonLeft<=0){
      buttonLeft=0;
    }
    if (buttonTop<=0){
      buttonTop=0
    }
    if (buttonTop + 50 >= this.data.windowHeight){
      buttonTop = this.data.windowHeight-50;
    }
    let top = 'top'+pen;
    let left = 'left'+pen;
    console.log('move '+pen+' top='+buttonTop+' left='+buttonLeft)
    this.setData({
      [top]: buttonTop,
      [left]: buttonLeft
    })
  },
  buttonEnd: function (e) {
    let pen = e.currentTarget.dataset.pen;
    let top = 'top'+pen;
    let left = 'left'+pen;
    let topok = 'top'+pen+'ok';
    let leftok = 'left'+pen+'ok';
    let buttonTop = 0;
    let buttonLeft = 0;
    let buttonTopOK = 0;
    let buttonLeftOK = 0;
    let offset = 50;
      buttonTop = this.data[top];
      buttonLeft = this.data[left];
      buttonTopOK = this.data[topok];
      buttonLeftOK = this.data[leftok];
    console.log('end '+pen+' top='+buttonTop+' left='+buttonLeft)
    
    if(buttonTop<=buttonTopOK+offset && buttonTop>=buttonTopOK-offset && buttonLeft<=buttonLeftOK+offset && buttonLeft>=buttonLeftOK-offset){
      wx.showToast({
        title: '操作成功！',  // 标题
        icon: 'success',   // 图标类型，默认success
        duration: 1500   // 提示窗停留时间，默认1500ms
      })
      let count = this.data.count++;
      console.log('end OK '+pen+' top='+buttonTop+' left='+buttonLeft+' count='+count)
    }



  },
  resetKey(){
    this.setData({
    //   x1: 244,
    // y1: 32,
    // x2: 102,
    // y2: 16,
    // x3: 179,
    // y3: 60,
    // x4: 311,
    // y4: 12,
      isok:'',
      play1:0,
    play2:0,
    play3:0,
    play4:0,
    })
    wx.redirectTo({
      url: '../lock/test4',
    })
  },
  isOk(){
    let isok = this.data.isok;
    var words = isok.split(",");
    words = this.uniqueStr(words);
    isok = words.join(',');
    let tip = '再想想!';
    if(isok == '2,4,1,3,'){
      console.log('锁开了!')
      tip = '锁开了!';
    }
    else{
      console.log('2413再想想!'+ isok)
    }
    wx.showModal({
      title: '提示',
      content: tip,
      success: (res) => {
          // wx.switchTab({
          //   url: '/pages/strategyList/strategyList'
          // })                
      }
    })
  },
    //播放按钮事件
    playMusic : function(){
      audioCxt.play();
    },
  uniqueStr (array){
    return array.filter(function(el, index, arr) {
    return index == arr.indexOf(el);
    });
  },

})